package com.app.backend.config.apiVersion;

import com.app.backend.common.annotation.AnoApiVersion;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;


/**
 * @program: CustomRequestMappingHandlerMapping
 * @description:
 * @author: Zhun.Xiao
 * @create: 2020-02-08 15:00
 **/
public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

    @Override
    protected RequestCondition<ApiVersionCondition> getCustomTypeCondition(Class<?> handlerType) {
        AnoApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, AnoApiVersion.class);
        return createCondition(apiVersion);
    }

    @Override
    protected RequestCondition<ApiVersionCondition> getCustomMethodCondition(Method method) {
        AnoApiVersion apiVersion = AnnotationUtils.findAnnotation(method, AnoApiVersion.class);
        return createCondition(apiVersion);
    }

    private RequestCondition<ApiVersionCondition> createCondition(AnoApiVersion apiVersion) {
        return apiVersion == null ? null : new ApiVersionCondition(apiVersion.value());
    }


}